/**
 * 
 */
package com.hudie;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import java.util.Date;
import java.util.List;
import java.util.Properties;


import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.mortbay.log.Log;
import org.xml.sax.InputSource;

import java.util.logging.Logger;

@SuppressWarnings("serial")
/*
 * *
 * 
 * @author Liyun
 */
public class WeatherServlet extends HttpServlet {
	private static List<List<String>> weatherList = null;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain; charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String weather = ParseWeatherJsonData.getCityWeather();
		try {
			Properties props = new Properties();
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage msg = new MimeMessage(session);
			

			msg.setFrom(new InternetAddress("liyun8552@gmail.com", "Admin"));
			msg.addRecipients(RecipientType.TO, InternetAddress
					.parse("13874915845@139.com"));
			
			msg.setSubject(new Date().toString());
			msg.setText(weather);

			msg.setReplyTo(InternetAddress
					.parse("hi@huangjinbb.appspotmail.com"));
			Transport.send(msg);
			
			Log.info("Message send");
			Log.warn("warn");

			// }
			// pm.close();

		} catch (AddressException e) {
			// ...
		} catch (MessagingException e) {
			// ...
			// }
		}

	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String args[]) throws IOException {
		URL url = new URL(
				"http://www.google.com/ig/api?weather=TianJin&hl=zh-cn");

		// 读取源码
		BufferedReader reader = new BufferedReader(new InputStreamReader(url
				.openStream(), "gb2312"));
		String xml = "";
		String line = "";
		while ((line = reader.readLine()) != null)
			xml += line;
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			SAXReader saxReader = new SAXReader();

			InputSource is = new InputSource();
			is.setByteStream(new ByteArrayInputStream(xml.getBytes()));
			sp.parse(is, saxReader);

			weatherList = saxReader.getWeathList();

			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			reader.close();
		}
		try {
			// read eac h person's info and compare,then send one by one
			// PersistenceManager pm = PMF.get().getPersistenceManager();
			// String query = "select from " + UserData.class.getName();
			// List<UserData> userdatas = (List<UserData>) pm.newQuery(query)
			// .execute();
			// if (userdatas.isEmpty()) {
			// No one
			// } else {
			Properties props = new Properties();
			
			props.put("mail.smtp.host","mail.yeah.net");//存储发送邮件服务器的信息  
			props.put("mail.smtp.auth","true");//同时通过验证
			
			
			
			
			
			Session session = Session.getInstance(props);
			//session.setDebug(true);
			
			MimeMessage msg = new MimeMessage(session);
			MimeMultipart mp = new MimeMultipart();

			msg.setFrom(new InternetAddress("liyun8552@gmail.com", "Admin"));
			msg.addRecipients(RecipientType.TO, InternetAddress
					.parse("13874915845@139.com"));
			String subject = weatherList.toArray()[2].toString();

			msg.setSubject("GB2312");
			msg.setText("Helloworld");
			//BodyPart body = new MimeBodyPart();
			//body.setContent(weatherList.toString(),"text/plain;charset=GB2312"); // ！！！注意设置编码

			//mp.addBodyPart(body);
			//msg.setContent(mp);

			msg.setReplyTo(InternetAddress
					.parse("hi@huangjinbb.appspotmail.com"));
			Transport transport=session.getTransport("smtp");  
			//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码  
			transport.connect("smtp.yeah.net","test4321a@yeah.net","a1234test");  
			transport.sendMessage(msg,msg.getAllRecipients());
			//Transport.send(msg);
			transport.close();

			// }
			// pm.close();

		} catch (AddressException e) {
			// ...
		} catch (MessagingException e) {
			e.printStackTrace();// ...
			// }
		}catch(Exception e){
			e.printStackTrace();
		}
		System.exit(0);

	}

}
